package com.jcfk.eam.service.mould;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.core.utils.BeanUtils;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.dao.mould.MouldWorkRecordDao;
import com.jcfk.eam.domain.dto.mould.MouldLedgerOperateDTO;
import com.jcfk.eam.domain.dto.mould.MouldWorkRecordDTO;
import com.jcfk.eam.domain.po.mould.MouldWorkRecord;
import com.jcfk.eam.domain.query.mould.MouldWorkRecordQuery;
import com.jcfk.eam.enums.AssetKind;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 通用模具操作记录
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-11-13
 */
@Service
public class MouldWorkRecordService extends BaseService<MouldWorkRecordDao, MouldWorkRecord> {

    /**
     * EamModWorkRecord分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<MouldWorkRecordDTO> queryByPage(DataPage<MouldWorkRecordDTO> page, MouldWorkRecordQuery query) {
        // 分页查询
        query.setProductCode(StringUtils.isEmpty(query.getProductCode()) ? query.getProductCode() : query.getProductCode() + "%");
        query.setMaterialCode(StringUtils.isEmpty(query.getMaterialCode()) ? query.getMaterialCode() : query.getMaterialCode() + "%");
        query.setMaterialName(StringUtils.isEmpty(query.getMaterialName()) ? query.getMaterialName() : query.getMaterialName() + "%");
        query.setMouldCode(StringUtils.isEmpty(query.getMouldCode()) ? query.getMouldCode() : "%" + query.getMouldCode() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamModWorkRecord是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(MouldWorkRecordDTO dto) {
        QueryWrapper<MouldWorkRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);

        return RetResult.ok();
    }

    /**
     * 插入通用模具操作记录
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(MouldWorkRecordDTO dto) {

        // 保存
        MouldWorkRecord item = new MouldWorkRecord();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }


    /**
     * 插入通用模具操作记录
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertWork(MouldLedgerOperateDTO dto, String status) {

        MouldWorkRecord eamModWorkRecord = new MouldWorkRecord();
        eamModWorkRecord.setWorkTime(new Date());
        eamModWorkRecord.setProjectCode(dto.getProjectCode());
        eamModWorkRecord.setProcessCode(dto.getProcessCode());
        eamModWorkRecord.setWorkKind(status);
        eamModWorkRecord.setMaterialId(dto.getMdmMaterialDTO().getId());
        eamModWorkRecord.setMaterialCode(dto.getMdmMaterialDTO().getMaterialCode());
        eamModWorkRecord.setMaterialName(dto.getMdmMaterialDTO().getMaterialName());
        eamModWorkRecord.setAssetKind(AssetKind.MOULD.getValue());
        eamModWorkRecord.setMouldCode(dto.getCode());
        this.setDefaultValue(eamModWorkRecord);
        super.save(eamModWorkRecord);
    }

    /**
     * 更新通用模具操作记录
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(MouldWorkRecordDTO dto) {

        // 保存
        MouldWorkRecord item = new MouldWorkRecord();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除通用模具操作记录(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(MouldWorkRecordDTO dto) {

        MouldWorkRecord item = new MouldWorkRecord();

        this.setDefaultValue(item);
        item.setDeleted(1);

        QueryWrapper<MouldWorkRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(MouldWorkRecord item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取通用模具操作记录信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public MouldWorkRecordDTO get(String id) {
        // 查询数据
        MouldWorkRecord item = this.baseMapper.selectById(id);
        MouldWorkRecordDTO dto = new MouldWorkRecordDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamModWorkRecordDTO>
     */
    public List<MouldWorkRecordDTO> getList(MouldWorkRecordQuery query) {
        // 查询条件
        QueryWrapper<MouldWorkRecord> qw = new QueryWrapper<>();
        // 查询数据
        List<MouldWorkRecord> list = super.list(qw);

        return convertList(list, MouldWorkRecordDTO.class);
    }

    /**
     * 根据主键获取通用模具操作记录信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        MouldWorkRecord item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }
}

